<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2016/8/11
 * Time: 14:01
 */
namespace SDXApi\Model;
use Think\Model;

class EvaluateModel extends Model
{
    protected $tableName = 'comment';
    
    /**
     * 获取商品评价列表
     * @param unknown $goods_id
     * @param number $limit
     */
    public function get_goods_evaluate($goods_id,$limit=0){

        
        $comment_goods = M('comment_goods')->alias('a')->join("left join ".C('DB_PREFIX')."comment b on a.comment_id=b.comment_id left join (select comment_id,img_name from sdx_comment_img group by comment_id) as c on a.comment_id=c.comment_id")->where(array('a.goods_id'=>$goods_id,'b.comment_status'=>'1','a.is_delete'=>0))->order("a.sort desc,img_name1 desc,content1 desc,b.add_time desc")->field("*,a.comment_rank as comment_rank,if(img_name<>'',1,0) as img_name1,if(b.content<>'',1,0) as content1")->limit($limit)->select();

        $comment_goods_count = M('comment_goods')->alias('a')->join("left join ".C('DB_PREFIX')."comment b on a.comment_id=b.comment_id")->where(array('a.goods_id'=>$goods_id,'b.comment_status'=>'1'))->order("b.add_time desc")->field('*,a.comment_rank as comment_rank')->count();
        
        $comment_list_a['count']=$comment_goods_count;
        
        $comment_list=array();
        foreach($comment_goods as $li){
            $comment=array();
            $wechat_user = M("wechat_user")->field("headimgurl,nickname")->where(array("ect_uid"=>$li['user_id']))->find();
            $comment['user_name']=$wechat_user['nickname'];
            $comment['comment_rank']=$li['comment_rank'];
            $comment['content']=$li['content'];
            $comment['add_time']=local_date('m月d日 H:i:s',$li['add_time']);
            $comment['is_reply']=$li['reply_time']?1:0;
            $comment['reply_time']=local_date('m月d日 H:i:s',$li['reply_time']);
            $comment['reply_user']=$li['reply_user'];
            $comment['reply_content']=$li['reply_content'];
            $comment['headImage']=str_replace("http://", "https://", $wechat_user['headimgurl']);
            
            if(empty($wechat_user['nickname'])){
                $comment['user_name']=$li['user_name'];
            }

            if(mb_strlen($comment['user_name'],'utf-8')>11){
                $len = mb_strlen($comment['user_name'],'utf-8');
                $str1 = mb_substr($comment['user_name'],0,3,'utf-8');
                $str2 = mb_substr($comment['user_name'],$len-4,4,'utf-8');
                $comment['user_name']=$str1.'****'.$str2;
            }
            
            
            $comment_img = M('comment_img')->where(array("comment_id"=>$li['comment_id']))->select();
            foreach($comment_img as $comment_img_item){
                $goods_img=array();
                $goods_img['img_name']=$comment_img_item['img_name'];
                $comment['imgs'][]=$goods_img;
            }
            
            $comment_list[]=$comment;
        }
        $comment_list_a['goods']=$comment_list;
        
        
        
        return $comment_list_a;
         
    }
    
    
    
    
}